from django.http import JsonResponse
from django.views import View
from app.models import *
import re


class ConfirmAPIView(View):  # 真实认证接口
    def post(self, request):
        try:
            id = request.POST.get('id', '暂无数据')  # 获取id
            diplomaImage = request.POST.get('diplomaImage', '暂无数据')  # 毕业证书图片
            carImage = request.POST.get('carImage', '暂无数据')  # 机动车行驶证图片
            houseImage = request.POST.get('houseImage', '暂无数据')  # 房产证图片
            salaryImage = request.POST.get('salaryImage', '暂无数据')  # 工资流水图片
            if id == '暂无数据' and diplomaImage == '暂无数据' and carImage == '暂无数据' and houseImage == '暂无数据' and salaryImage == '暂无数据':
                return JsonResponse({'code': '400', 'msg': '请填写正确的数据'})
            data = User.objects.select_related('userinfo').get(id=id)  # 查找对象
            if not diplomaImage == '暂无数据':
                data.userinfo.diplomaImage = diplomaImage  # 保存毕业证
            if not carImage == '暂无数据':
                data.userinfo.carImage = carImage  # 保存行驶证
            if not houseImage == '暂无数据':
                data.userinfo.houseImage = houseImage  # 保存房产证
            if not salaryImage == '暂无数据':
                data.userinfo.salaryImage = salaryImage  # 保存工资流水
            data.userinfo.save()  # 保存数据
            return JsonResponse({'code': '200', 'msg': '认证成功'})
        except Exception as e:
            return JsonResponse({"code": "500", "msg": f"服务器错误: {str(e)}"}, status=500)
